package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.DashangDialog
import hlhj.fhp.supreme.fragment.OptionDetailAudioFgm
import hlhj.fhp.supreme.fragment.OptionDetailTextFgm
import hlhj.fhp.supreme.fragment.OptionDetailVideoFgm
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.OptionDetailBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_option_detail_aty.*

class OptionDetailAty : BaseAty(), SpringView.OnFreshListener {
    override fun onLoadmore() {
    }

    override fun onRefresh() {
        /*访问详情*/

    }
    private lateinit var videoFgm:Fragment
    private lateinit var audioFgm:Fragment
    private lateinit var textFgm:Fragment
    private var isLike=0
    private var isFollow=0
    private var tittle=""
    private var des=""
    private var cat_name="  "
    private var img=""
    override fun getContentId(): Int {
        return R.layout.activity_option_detail_aty
    }

    override fun initView() {
        initSp()
            LogUtil.logi(intent.getIntExtra("id",0).toString())
        when(intent.getIntExtra("type",0)){
            1->{
                textFgm=OptionDetailTextFgm.newInstence(intent.getIntExtra("id",0))
                val transaction = supportFragmentManager.beginTransaction()
                transaction.add(R.id.rp,textFgm)
                transaction.show(textFgm)
                transaction.commit()
            }
            2->{
                videoFgm=OptionDetailVideoFgm.newInstence(intent.getIntExtra("id",0))
                val transaction = supportFragmentManager.beginTransaction()
                transaction.add(R.id.rp,videoFgm)
                transaction.show(videoFgm)
                transaction.commit()
            }
            3->{
                audioFgm=OptionDetailAudioFgm.newInstence(intent.getIntExtra("id",0))
                val transaction = supportFragmentManager.beginTransaction()
                transaction.add(R.id.rp,audioFgm)
                transaction.show(audioFgm)
                transaction.commit()
            }
        }
        /*访问详情*/
        /*OkGo.post<OptionDetailBean>(Urls.BaseUrl+Urls.OPTIN_DETAIL)
                .params("id",intent.getIntExtra("id",0).toString())
                .execute(object : JsonCallBack<OptionDetailBean>(this){
                    override fun onSuccess(response: Response<OptionDetailBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            isLike=bean?.data.is_agree
                            isFollow=bean?.data.is_favorite
                            tittle=bean?.data.title
                            des=bean?.data.desc
                            cat_name=intent.getStringExtra("cat_name")
                            img=bean?.data.img
                            btLike.text=bean?.data.agree.toString()
                            btComment.text=bean?.data.comment.toString()
                            when(isLike){
                                0->{
                                    btLike.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_zan),null,null)
                                }
                                1->{
                                    btLike.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_zan_choose),null,null)

                                }
                            }
                            when(isFollow){
                                0->{
                                    btFollow.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_shoucang),null,null)
                                }
                                1->{
                                    btFollow.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_shoucang_choose),null,null)
                                }
                            }
                        }else{
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })
     *//*   val transaction = supportFragmentManager.beginTransaction()
        transaction.add(R.id.rp,videoFgm)
        transaction.add(R.id.rp,audioFgm)
        transaction.hide(videoFgm)
        transaction.hide(audioFgm)
        transaction.commit()*//*
//        MyUtils.trans(supportFragmentManager,R.id.rp,videoFgm)*/
    }

    private fun initSp() {
    }

    override fun onResume() {
        super.onResume()
        OkGo.post<OptionDetailBean>(Urls.BaseUrl+Urls.OPTIN_DETAIL)
                .params("id",intent.getIntExtra("id",0).toString())
                .execute(object : JsonCallBack<OptionDetailBean>(this){
                    override fun onSuccess(response: Response<OptionDetailBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            isLike=bean?.data.is_agree
                            isFollow=bean?.data.is_favorite
                            tittle=bean?.data.title
                            des=bean?.data.desc
                            cat_name=intent.getStringExtra("cat_name")
                            img=bean?.data.img
                            btLike.text=bean?.data.agree.toString()
                            btComment.text=bean?.data.comment.toString()
                            when(isLike){
                                0->{
                                    btLike.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_zan),null,null)
                                }
                                1->{
                                    btLike.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_zan_choose),null,null)

                                }
                            }
                            when(isFollow){
                                0->{
                                    btFollow.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_shoucang),null,null)
                                }
                                1->{
                                    btFollow.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_shoucang_choose),null,null)
                                }
                            }
                        }else{
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })
        /*   val transaction = supportFragmentManager.beginTransaction()
           transaction.add(R.id.rp,videoFgm)
           transaction.add(R.id.rp,audioFgm)
           transaction.hide(videoFgm)
           transaction.hide(audioFgm)
           transaction.commit()*/
//        MyUtils.trans(supportFragmentManager,R.id.rp,videoFgm)
    }
    override fun initListener() {
        /*打赏*/
        btGift.setOnClickListener {
            val dashangDialog = DashangDialog(this,1,intent.getIntExtra("id",0))
            dashangDialog.show()

        }
        btExit.setOnClickListener { finish() }
        btLike.setOnClickListener {
                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.LIKE)
                            .params("id",intent.getIntExtra("id",0).toString())
                            .params("type",1)
                            .execute(object :JsonCallBack<BaseBean>(this){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        when(isLike){
                                            0->{
                                                isLike=1
                                                btLike.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_zan_choose),null,null)
                                                btLike.text=(btLike.text.toString().toInt()+1).toString()
                                            }
                                            1->{
                                                isLike=0
                                                btLike.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_zan),null,null)
                                                btLike.text=(btLike.text.toString().toInt()-1).toString()
                                            }
                                        }

                                    }
                                }
                            })
        }

        btFollow.setOnClickListener {
            OkGo.post<BaseBean>(Urls.BaseUrl+Urls.FOLLOW)
                    .params("title",tittle)
                    .params("cat_name",cat_name)
                    .params("id",intent.getIntExtra("id",0).toString())
                    .params("type",1)
                    .params("img",img)
                    .execute(object :JsonCallBack<BaseBean>(this){
                        override fun onSuccess(response: Response<BaseBean>?) {
                            val bean = response?.body()
                            if (bean?.code==1){
                                when(isFollow){
                                    0->{
                                        isFollow=1
                                        MyUtils.toast("收藏成功")
                                        btFollow.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_shoucang_choose),null,null)
                                    }
                                    1->{
                                        isFollow=0
                                        MyUtils.toast("取消收藏成功")
                                        btFollow.setCompoundDrawablesWithIntrinsicBounds(null,resources.getDrawable(R.mipmap.ic_shoucang),null,null)
                                    }
                                }
                            }
                        }
                    })

        }//收藏
        radioGroup.check(R.id.rgVideo)

        btComment.setOnClickListener {
            val  i= Intent(this, OptionCommentAty::class.java)
//            MyUtils.toast("传入的id${intent.getIntExtra("id",0).toString()}")
            i.putExtra("oid",intent.getIntExtra("id",0).toString())
            i.putExtra("type",1)
            i.putExtra("tittle",tittle)
            i.putExtra("des",des)
            i.putExtra("img",img)
            startActivity(i)
        }
    }
}
